package com.zghhost.codedemo.other;

import com.zghhost.codedemo.util.BitUtil;
import org.apache.commons.lang.StringEscapeUtils;

import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/4/28 11:02
 */
public class Temp {
    public static void main(String[] args)throws Exception{
        String s = "123\n\r\t\b\f\\\'\"";
        String es = StringEscapeUtils.escapeSql(StringEscapeUtils.escapeJava(s));
        System.out.println(es);
        String res = StringEscapeUtils.unescapeJava(es);
        System.out.println(res);

        char c = 0x08;
        System.out.println(">>>"+c+"<<<");
    }

    public static void lock()throws Exception{
    }

    public static void queue(){

    }

    public static void arraylinkedBlockingQueue()throws Exception{
        ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(5);
        LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<>(5);
        for(Integer i=0,ic=5;i<ic;i++){
            abq.put(i);
            lbq.put(i);
        }

        while(!abq.isEmpty()){
            System.out.println(abq.take());
        }

        System.out.println();

        while (!lbq.isEmpty()){
            System.out.println(lbq.take());
        }
    }

    public static void concurrentLinkedQueue(){
        ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue();
        for(Integer i=0,ic=5;i<ic;i++){
            queue.add(i);
        }

        while (!queue.isEmpty()){
            System.out.println(queue.poll());
        }
    }

    public static void vector(){
        Vector<Integer> vector = new Vector<>();

        vector.add(1);

        vector.get(0);
    }

    public static void arrayList(){
        ArrayList<Integer> list = new ArrayList<>();

        list.add(1);

        list.get(0);
    }

    public static void linkedHashMap(){
        LinkedHashMap<HashBean,Integer> lmap = new LinkedHashMap<HashBean,Integer>();

        for(int i=0;;){
            lmap.put(new HashBean(i),Integer.valueOf(i));

            if(++i >= 100){
                break;
            }
        }

        lmap.put(new HashBean(101),Integer.valueOf(101));

        System.out.println(lmap.get(lmap.keySet().iterator().next()));

//        for (HashBean k : lmap.keySet()){
//            System.out.println(k.toString()+":"+lmap.get(k));
//        }

        System.out.println("----------");

        HashMap<HashBean,Integer> map = new HashMap();

        for(int i=0;;){
            map.put(new HashBean(i),Integer.valueOf(i));

            if(++i >= 100){
                break;
            }
        }

        map.put(new HashBean(101),Integer.valueOf(101));

        System.out.println(map.get(map.keySet().iterator().next()));

//        for (HashBean k : map.keySet()){
//            System.out.println(k.toString()+":"+map.get(k));
//        }
    }

    public static void hashMapHashFunction(){
        //这里看下hashMap hash函数的作用 虽然例子没能实验出结果 但是意思明白就行了
        String binX = "01111111111111110000000001000000";
        String binY = "01111111101111110000000001000000";

        String binL = "00000000000000000000000001000000";

        int x = Integer.valueOf(binX,2);
        int y = Integer.valueOf(binY,2);

        int l = Integer.valueOf(binL,2);

        System.out.println("     binX="+binX);
        System.out.println("     binY="+binY);
        System.out.println("binLength="+binL);

        System.out.println("x="+x);
        System.out.println("y="+y);
        System.out.println("length="+l);

        System.out.println("x & length="+(x & l));
        System.out.println("y & length="+(y & l));

        System.out.println("(x ^ (x >>> 16)) & length="+((x ^ (x >>> 16)) & l));
        System.out.println("(y ^ (y >>> 16)) & length="+((y ^ (y >>> 16)) & l));

        //如果没有hashmap的hash函数干预 x y 就会产生碰撞
    }

    public static void hashTable(){
        System.out.println(0x7FFFFFFF);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(BitUtil.prefix(Integer.toBinaryString(0x7FFFFFFF),'0',32));
        System.out.println(BitUtil.prefix(Integer.toBinaryString((1)),'0',32));
        System.out.println(BitUtil.prefix(Integer.toBinaryString((1 >>> 16)),'0',32));

        for(int i=0,ic=10;i<ic;i++){
            System.out.println(i + "  " + (i & 0x7FFFFFFF));
            System.out.println(i + "  " + (i ^ (i>>>16)));
        }
        for(int i=Integer.MAX_VALUE,ic=Integer.MAX_VALUE-10;i>=ic;i--){
            System.out.println(i + "  " + (i & 0x7FFFFFFF));
            System.out.println(i + "  " + (i ^ (i>>>16)));
        }

        Hashtable<HashBean,Integer> tmap = new Hashtable();

        for(int i=0;;){
            tmap.put(new HashBean(i),Integer.valueOf(i));

            if(++i >= 100){
                break;
            }
        }

        tmap.put(new HashBean(101),Integer.valueOf(101));

        System.out.println(tmap.get(tmap.keySet().iterator().next()));

        System.out.println("----------");
    }
}
